package org.wanghong.finance.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wanghong.aftermarket.domain.Customer;
import org.wanghong.aftermarket.mapper.CustomerMapper;
import org.wanghong.basic.service.impl.BaseServiceImpl;
import org.wanghong.finance.domain.Deposit;
import org.wanghong.finance.dto.DepositPayDto;
import org.wanghong.finance.mapper.DepositMapper;
import org.wanghong.finance.service.IDepositService;
import org.wanghong.mkt.domain.Business;
import org.wanghong.mkt.domain.Clue;
import org.wanghong.mkt.mapper.BusinessMapper;
import org.wanghong.mkt.mapper.ClueMapper;
import org.wanghong.order.domain.Order;
import org.wanghong.order.domain.OrderProduct;
import org.wanghong.order.mapper.OrderMapper;
import org.wanghong.order.mapper.OrderProductMapper;
import org.wanghong.org.domain.Employee;
import org.wanghong.product.domain.Product;
import org.wanghong.product.mapper.ProductMapper;

import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-08-26
 */
@Service
public class DepositServiceImpl extends BaseServiceImpl<Deposit> implements IDepositService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private DepositMapper depositMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ProductMapper productMapper;

    @Transactional
    @Override
    public void pay(DepositPayDto depositPayDto) {

        Employee loginUser = depositPayDto.getLoginUser();
        /*System.out.println("==========");
        System.out.println(loginUser.getId());*/
        Long businessId = depositPayDto.getBusinessId();
        System.out.println("businessId:" + businessId);
        Business business = businessMapper.loadById(businessId);
        System.out.println("business:" + business);
        Long clueId = business.getClueId();
        Clue clue = clueMapper.loadById(clueId);

        // 创建客户，
        Customer customer = new Customer();
        customer.setName(clue.getFullName());
        customer.setTel(clue.getPhone());
        customer.setEmail(clue.getEmail());
        customer.setState(0);
        customer.setCustomersourceId(clue.getSource());
        /* private Long jobId;
        private Long salarylevelId;*/
        customer.setSellerId(loginUser.getId());
        customer.setInputuserId(loginUser.getId());
        customerMapper.save(customer);
        //创建订单
        Order order = new Order();
        order.setSn(UUID.randomUUID().toString());
        order.setBusinessId(businessId);
        order.setBusinessName(business.getName());
        order.setProductId(business.getProductId());
        order.setBusinessName(business.getProductName());
        //价格就是产品价格-减去活动价 @TODO
        order.setPrice(business.getProductPrice());
        order.setCustomerId(customer.getId());
        order.setCustomerName(customer.getName());
        orderMapper.save(order);
        // 订单产品
        OrderProduct op = new OrderProduct();
        Product product = productMapper
                .loadById(business.getProductId());
        /*Product product = productMapper
                .loadById(business.getId());*/
        /*System.out.println("=========");
        System.out.println(product);*/
        BeanUtils.copyProperties(product,op);
        op.setId(null);
        op.setOrderId(order.getId());
        orderProductMapper.save(op);
        // 订单活动
        List<Map<String,Long>> list = new ArrayList<>();
        depositPayDto.getActivityIds().forEach(acitityId->{
            Map<String,Long> map = new HashMap<>();
            map.put("orderId",order.getId());
            map.put("activityId",acitityId);
            list.add(map);
        });
        orderMapper.batchOrderActivitys(list);
        // 收定金

        Deposit deposit = new Deposit();
        deposit.setName(customer.getName()
                +"缴纳了"+depositPayDto.getPrice()+"定金");
        deposit.setCustomerId(customer.getId());
        deposit.setCustomerName(customer.getName());
        deposit.setDeposit(depositPayDto.getPrice()+"");
        deposit.setProductId(product.getId());
        deposit.setProductName(product.getName());
        deposit.setBusinessId(businessId);
        deposit.setBusinessName(business.getName());
        deposit.setState(1);
        depositMapper.save(deposit);
    }
}
